package zuel.innovation.disley.homework.controller;

import cn.fabrice.common.constant.BaseConstants;
import cn.fabrice.common.pojo.BaseResult;
import cn.fabrice.common.pojo.DataResult;
import cn.fabrice.jfinal.annotation.Param;
import cn.fabrice.jfinal.annotation.ValidateParam;
import cn.fabrice.jfinal.ext.cros.interceptor.CrossInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Kv;
import zuel.innovation.disley.common.module.*;
import zuel.innovation.disley.homework.service.HomeworkService;
import zuel.innovation.disley.homework.service.HomeworkUserService;
import zuel.innovation.disley.user.service.UserService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

// 路由扫描
@Before(CrossInterceptor.class)
@Path("/homework")
@ValidateParam


public class HomeworkController extends Controller {

    @Inject
    HomeworkUserService homeworkUserService;
    @Inject
    UserService service;
    @Inject
    HomeworkService homeworkService;



    /**
     * 管理员获取某一次培训的某一用户的作业
     * @param trId 培训编号
     * @param uId 用户编号
     */
    @Param(name = "trId",required = true)
    @Param(name = "uId",required = true)
    public void getUserHomework(int trId,int uId){
        Kv cond=Kv.by("trId",trId);
        cond.set("uId",uId);
        HomeworkUser userHomework=homeworkUserService.get(cond,"getUserHomework");
        User user=service.get(cond,"getUserById");
        Date update=userHomework.getHuUpdate();
        HashMap<String,Object> data = new HashMap<>();
        data.put("u_id",userHomework.getUId());
        data.put("u_name",user.getUName());
        if(update==null){
            data.put("homework_state","未提交");
        }else {
            data.put("homework_state","已提交");
        }
        data.put("time",update);
        renderJson(DataResult.data(data));
    }



    /**
     * 管理员发布培训作业
     * @param trId 培训编号
     * @param hText 作业内容
     * @throws ParseException
     */
    @Param(name = "trId",required = true)
    @Param(name = "hText",required = true)
    public void addHomework(int trId,String hText) throws ParseException {
        Homework homework=new Homework();
        homework.setTrId(trId);
        homework.setHText(hText);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date hCreate=df.parse(df.format(System.currentTimeMillis()));
        homework.setHCreate(hCreate);
        homework.setHDeleted(0);
        boolean flag=homework.save();
        renderJson(flag? BaseResult.ok():BaseResult.fail());
        int hId=homework.getHId();
        Kv cond=Kv.by("trId",trId);
        List<User> userList=service.list(cond,"selectUserByTrid");
        for(User i:userList){
            int uId=i.getUId();
            HomeworkUser homeworkUser=new HomeworkUser();
            homeworkUser.setHId(hId);
            homeworkUser.setUId(uId);
            homeworkUser.setHuCreate(hCreate);
            homeworkUser.setHuDeleted(0);
            homeworkUser.save();
        }
    }



    /**
     * 用户上传文本作业（文件作业是文件上传功能）
     * @param trId 培训编号
     * @param huUpload 作业上传
     * @throws ParseException
     */
    @Param(name = "trId",required = true)
    @Param(name = "huUpload",required = true)
    public void addHomeworkUser(int trId,String huUpload) throws ParseException {
        int uId=getAttr(BaseConstants.ACCOUNT_ID);
        Kv cond=Kv.by("uId",uId);
        cond.set("trId",trId);
        HomeworkUser homeworkUser=homeworkUserService.get(cond,"addHomeworkUser");
        homeworkUser.setHuUpload(huUpload);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date huUpdate=df.parse(df.format(System.currentTimeMillis()));
        homeworkUser.setHuUpdate(huUpdate);
        renderJson(homeworkUser.update()?BaseResult.ok():BaseResult.fail());
        
    }



    /**
     * 管理员发布作业评价
     * @param trId 培训编号
     * @param uId 用户编号
     * @param huComment 评价内容
     */
    @Param(name = "trId",required = true)
    @Param(name = "uId",required = true)
    @Param(name = "huComment",required = true)
    public  void addHomeworkJudge(int trId,int uId,String huComment){
        Kv cond=Kv.by("uId",uId);
        cond.set("trId",trId);
        HomeworkUser homeworkUser=homeworkUserService.get(cond,"addHomeworkUser");
        homeworkUser.setHuComment(huComment);
        renderJson(homeworkUser.update()?BaseResult.ok():BaseResult.fail());
    }




    /**
     * 用户获取自己作业的评价
     * @param trId 培训编号
     */
    @Param(name = "trId",required = true)
    public void getUserHomeworkJudge(int trId){
        Kv cond=Kv.by("trId",trId);
        int uId=getAttr(BaseConstants.ACCOUNT_ID);
        cond.set("uId",uId);
        HomeworkUser userHomework=homeworkUserService.get(cond,"getUserHomeworkJudge");
        renderJson(DataResult.data(userHomework));
    }


    /**
     * 判断此次培训是否发布了作业
     */
    @Param(name = "trId",required = true)
    public void isReleaseHomework(int trId){
        Kv cond=Kv.by("trId",trId);
        Homework homework=homeworkService.get(cond,"isReleaseHomework");
        if (homework==null){
            renderJson(BaseResult.ok("本次培训暂未发布作业"));
        }else {
            renderJson(DataResult.data(homework));
        }

    }





}
